package coe.pumbaa
import coe.pumbaa.Category
import coe.pumbaa.User
import coe.pumbaa.Role

class AuthenticationManagerService {

    def stringManagerService
    def springSecurityService
    
    def isAdmin(def user){
        if(user != null){
            if(user.role.level == 0) //admin
            return true
        }
        return false
    }
    
    def getCategoriesList(def user, def params){
        def categoryInstanceList = []
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        params.sort = "title"
        params.order = "desc"
        def c = Category.createCriteria()
        Category.list().each{
            println it.title + " : " + it.allowView
        }
        if(user != null){
            categoryInstanceList = c.list() {
                like 'allowView', '%' + user.role.id + '%'
            }
        }else{
            def annonymous = Role.findByLevel(4)
            
            if(annonymous){
                categoryInstanceList = c.list() {
                    like 'allowView', '%' + annonymous.id + '%'
                }
            }
        }
        return [count:categoryInstanceList.size(), list:filteredWithMaxAndOffset(categoryInstanceList,params)]
    }
    
    def getPostList(def user){
        def postList = []
        def posts = Post.list(sort:"timestamp", order:'desc')
        posts.each{
            if(postList.size() < 10){
                if(stringManagerService.checkObjectInString(user.role,it.category.allowView))
                postList.add(it)
            }else{
                return postList
            }
        }
        return postList
    }
    
    def getPostSubList(def user,def params){
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        
        def max = params.int('max')
        def offset = params.int('offset')
        
        max = Math.min(max ?max: 25, 100)
        offset = (offset && offset>0) ? offset : 0
        
        def postList = []
        
        def idList = stringManagerService.getMemberListInString(user.subscribe)
        for(int i = 0;i<idList.size();i++){
            if(i >= offset){
                if(postList.size() < max){
                    postList.add(Post.get(idList[i]))
                }
            }
        }
        return postList
    }
    
    def filteredWithMaxAndOffset(def list, def params){
        def max = params.int('max')
        def offset = params.int('offset')
        
        max = Math.min(max ?max: 25, 100)
        offset = (offset && offset>0) ? offset : 0
        List l = list //Loads the complete list
        int total = l.size()
        int upperLimit = findUpperIndex(offset, max, total)
        def filtered = []
        println "offset: " + offset + ", max: " + max
        if(upperLimit > -1){
            filtered = l.getAt(offset..upperLimit)
        }
        return filtered
    }
 
    private int findUpperIndex(int offset, int max, int total) {
        max = offset + max - 1
        if (max >= total) {
            max -= max - total + 1
        }
        return max
    }
    
    def getAnonymousUser(){
        def user = User.findByEmail("anonymous")
        return user
    }
    
    def getNumberOfRequestUser(){
        def count = 0
        def user = springSecurityService.currentUser
        def userList = User.findAllByStatus(User.PENDING_USER)
        userList.each{
            if(!stringManagerService.checkObjectInString(user,it.voter)){
                count++
            }
        }
        return count
    }
}
